﻿using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public abstract class DataServiceBase 
{
    protected Dictionary<int, GameData> GameDataDic;
    public int currentGameDataID { get; private set; }//当前存档ID

    string DataKey = "GameData";
    public void ReadInitData()
    {
        //从硬盘里面读取文件缓存到CPU中      //获取Json
        string gameDataDicJson = PlayerPrefs.GetString(DataKey);
        //反序列化得到类型
        GameDataDic = JsonConvert.DeserializeObject<Dictionary<int, GameData>>(gameDataDicJson);
        if (GameDataDic == null)
        {
            GameDataDic = new Dictionary<int, GameData>();
        }
    }
    /// <summary>
    /// 更新当前游戏存档ID
    /// </summary>
    /// <param name="id"></param>
    public void UpdateCurrentGameDataID(int id)
    {
        currentGameDataID = id ;
    }
    #region 创建新的游戏存档
    public int CreateNewGameData()
    {
        int saveID = GenerateGameDataID();
        GameDataDic[saveID] = new GameData(saveID);
        return saveID;
    }
    public int OverrideGameData()
    {
        var lastGameData = GameDataDic.Last();
        int saveID = lastGameData.Key;
        GameDataDic[saveID] = new GameData(saveID);
        return saveID;
    }
    #endregion

    #region 获取存档

    /// <summary>
    /// 拿到最新存档
    /// </summary>
    /// <returns></returns>
    public GameData GetLastGameData()
    {
        if (GameDataDic == null|| GameDataDic.Count ==0)
        {
            return null;
        }
        GameData gameData = GameDataDic.Last().Value;
        if (gameData == null)
        {
            this.Warn("Fail to find Last GameData");
        }
        return gameData;
    }
    /// <summary>
    /// 获取某个存档
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public GameData GetOneGameData(int id)
    {
        if (GameDataDic.TryGetValue(id, out var gameData))
        {
            return gameData;
        }
        this.Warn($"Failed to Find this GameData of {id} in GameDataDic");
        return null;
    }
    #endregion
    #region 保存存档
    public void CacheAndSaveLastGameData()
    {
        this.Log("正在保存游戏...");
        CacheLastGameData();
        SaveGameData();
        this.Log("成功保存！存档数：" + GameDataDic.Count);
    }
    public void SaveGameData()
    {
        string dataJson = JsonConvert.SerializeObject(GameDataDic); 
        PlayerPrefs.SetString(DataKey,dataJson);
    }
  
    /// <summary>
    /// 缓存存档，但不写入硬盘
    /// </summary>
    /// <param name="gameDataID"></param>
    public abstract void CacheGameData(GameData gameData);
    public void CacheLastGameData()
    {
        var gameData = GameDataDic.Values.Last();
        if (gameData != null)
        {
            CacheGameData(gameData);
        }
    }

    #endregion

    #region 生成存档ID

    int tid = 0;
    private int GenerateGameDataID()
    {
        while (true)
        {
            if (!GameDataDic.ContainsKey(tid))
            {
                return tid;
            }
            if (tid > int.MaxValue)
            {
                tid = 0;
            }
            tid++;
        }

    }

    #endregion


}